package controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import db.DbConn;

import model.UserInfo;

public class Register extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	DbConn conn;

	@Override
	public void init() throws ServletException {
		conn = new DbConn();
		conn.setDbUrl(getServletContext().getInitParameter("dbUrl"));
		conn.setDbUser(getServletContext().getInitParameter("dbUser"));
		conn.setDbPass(getServletContext().getInitParameter("dbPass"));
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String userId = req.getParameter("id");
		String userName = req.getParameter("name");
		String userPw = req.getParameter("pw");
		String state = req.getParameter("state");
		String email = req.getParameter("email_prefix") + "@" + req.getParameter("email_postfix");
		String height = req.getParameter("height");
		UserInfo userinfo = new UserInfo(userId, userName, userPw, state, email, height);
		Boolean result = conn.registerUser(userinfo);
		if(true == result) {
			resp.setContentType("text/html");
			PrintWriter out = resp.getWriter();
			out.println("<script type='text/javascript'>");
			out.println("alert(' Register Complete!');");
			out.println("location.href='login.jsp';");
			out.println("</script>");
			out.flush();
		} else {
			resp.setContentType("text/html");
			PrintWriter out = resp.getWriter();
			out.println("<script type='text/javascript'>");
			out.println("alert(' Register Fail!');");
			out.println("location.href='login.jsp';");
			out.println("</script>");
			out.flush();
		}
	}
	
}
